Flash can construct variable names dynamically because the variable assignment and retrieval operations can evaluate an expression to get the variable name. This makes it possible to emulate arrays, dictionaries, and other data structures. For example, a 10-element array could be emulated by ten variables with the names
A_1 A_2 ... A_10
For example, the array:
Set Variable "name" & namecount = newname Set Variable "namecount" = namecount + 1
creates an array that looks like this:
name1 -> Gary name2 -> another name name3 -> someone else namecount -> 3
To find a particular name, you can use the following action:
Set Variable "index" = 1 Set Variable "found" = false Loop While (index <= namecount and not found) If (Eval("name" & index) eq nametofind) Set Variable "found" = true Else Set Variable "index" = index + 1 End If End Loop
In this action, when found
is true, index
contains the array index of the located name.
You can also emulate a two-dimensional array, such as this:
A_1_1 A_2_1 ... A_3_3
Obtaining the value of a dynamically named variable
Use the eval
function to obtain the value of a variable named by an expression. For example, the expression eval("Name")
is exactly the same as the expression Name
.
By specifying the name as an expression, the name of the variable to be obtained can be decided while the Flash movie is playing.
For example, the statement eval("Name"&Index)
lets you manage 10 names, where Index
is a variable that contains a number from 1 to 10. This expression obtains one of the variables Name1, Name2, Name3, ... depending on what the value of Index
is. In a loop, Index
can be updated each iteration to obtain all 10 names.
Checking variable and property values
Because action scripting is not a typed language, variables may be misassigned as integers or strings. If an integer is entered for a string variable, then Flash assigns a "true" value to the integer. If a string is entered for an integer variable, then Flash assigns a number equaling the length of the string. (For example, entering Name returns 4).